package com.example.home.bus.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.home.R;
import com.example.home.bus.bean.SubmitBean;
import com.example.home.tool.OKHttpTool;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.util.Map;

public class MakeNext3 extends AppCompatActivity {

    private TextView start;
    private TextView end;
    private TextView path;
    private TextView iphone;
    private TextView upPoint;
    private TextView time;
    private TextView offPlace;
    private Button btnLast;
    private Button btnSubmit;
    private String price;
    private String startValue;
    private String path1;
    private String endValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_make_next3);
        initView();

        Intent intent = getIntent();
        startValue = intent.getStringExtra("start");
        endValue = intent.getStringExtra("end");
        String selectedDate = intent.getStringExtra("selectedDateValue");
        String name = intent.getStringExtra("name");
        String iphNum = intent.getStringExtra("iphNum");
        String uponint1 = intent.getStringExtra("uponint1");
        String offplace1 = intent.getStringExtra("offplace1");
        path1 = intent.getStringExtra("path1");
        price = intent.getStringExtra("price");

        start.setText(startValue);
        end.setText(endValue);
        path.setText(name);
        upPoint.setText(uponint1);
        offPlace.setText(offplace1);
        iphone.setText(iphNum);
        time.setText(selectedDate);

        submitData();
    }



    private void submitData() {
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("start",startValue);
                    jsonObject.put("end",endValue);
                    jsonObject.put("path",path1);
                    jsonObject.put("price",price);
                    jsonObject.put("status",0);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                OKHttpTool.doNetRequest("prod-api/api/bus/order",
                        OKHttpTool.POST,
                        SubmitBean.class,
                        new OKHttpTool.NetCallback() {
                            @Override
                            public void onSuccess(Object obj) {
                                SubmitBean hotAttorneyBean = (SubmitBean) obj;
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (hotAttorneyBean.getCode() == 200){
                                            Toast.makeText(MakeNext3.this,"提交成功",Toast.LENGTH_SHORT).show();
                                            /*Intent intent = new Intent(MakeNext3.this, getFragmentManager().getClass());
                                            startActivity(intent);*/
                                            finishAllActivity();

                                        }else if (hotAttorneyBean.getCode() == 500){
                                            Toast.makeText(MakeNext3.this,"提交失败",Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }

                            @Override
                            public void onFailure(String msg) {

                            }
                        },jsonObject.toString());
            }
        });
    }

    private void finishAllActivity() {
        //传消息回去清空数据
        Intent intent = new Intent("bov_action3");
        MakeNext3.this.sendBroadcast(intent);
        //结束三个Activity
        Class activityThreadClass = null;

        try {
            activityThreadClass = Class.forName("android.app.ActivityThread");
            Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
            Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
            activitiesField.setAccessible(true);
            Map activities = (Map) activitiesField.get(activityThread);
            int i = 0;
            for (Object activityRecord : activities.values()) {
                Class activityRecordClass = activityRecord.getClass();
                Field activityField = activityRecordClass.getDeclaredField("activity");
                activityField.setAccessible(true);
                Activity activity = (Activity) activityField.get(activityRecord);

                if(activity instanceof MakeNext1  ){
                    activity.finish();
                }else if(activity instanceof MakeNext2  ){
                    activity.finish();
                }else if(activity instanceof MakeNext3  ){
                    activity.finish();
                }


                Log.e("activityThreadClass", "index:" + i + ",sum:" + activities.size()+ ", class name:" + activity.getClass().getName());
                i++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initView() {

        start = findViewById(R.id.start);
        time = findViewById(R.id.time);
        end = findViewById(R.id.end);
        path = findViewById(R.id.path);
        iphone = findViewById(R.id.iphone);
        upPoint = findViewById(R.id.up_point);
        offPlace = findViewById(R.id.off_place);
        btnLast = findViewById(R.id.btn_last);
        btnLast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MakeNext3.this.finish();
            }
        });
        btnSubmit = findViewById(R.id.btn_submit);

    }
}